package com.hg.onlineshop.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hg.onlineshop.entity.Comment;
import com.hg.onlineshop.entity.User;
import com.hg.onlineshop.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @GetMapping("/list")
    public List<Comment> findComments(Comment comment) {
        Integer productId = comment.getProductId();
        if (productId == null) {
            return null;
        }
        List<Comment> comments = commentMapper.selectCommentsByProductId(productId);
        return comments;
    }

    @PostMapping("/save")
    public String saveComments(@RequestBody Comment comment, HttpSession session) {
        Integer productId = comment.getProductId();
        if (productId == null) {
            return "E";
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "P";
        }
        comment.setMemberId(user.getId());
        comment.setSaveTime(new Date());
        commentMapper.insert(comment);
        return "S";
    }
}
